//
//  ZJDesSecMoreCityViewController.m
//  BreadTravel
//
//  Created by 张健 on 15/12/26.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "ZJDesSecMoreCityViewController.h"
#import "LRJDesFirCollectionViewCell.h"
#import "LRJDesFirDataModel.h"
#import "LRJDesSecCityViewController.h"
@interface ZJDesSecMoreCityViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>

@property(nonatomic,retain)NSMutableArray *dataArray;
@property(nonatomic,retain)UICollectionView *collectionView;

@end

@implementation ZJDesSecMoreCityViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置滚动方向
    //flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    //设置每个item的大小
    flowLayout.itemSize = CGSizeMake(WIDTH/2-23, 200);
    //设置最小行间距
    flowLayout.minimumLineSpacing = 20;
    //设置最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    //设置item与边界的距离  上左下右
    flowLayout.sectionInset = UIEdgeInsetsMake(15, 15, 20, 15);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT-49) collectionViewLayout:flowLayout];
    [self.view addSubview:self.collectionView];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.970 green:0.970 blue:0.920 alpha:1.000];
    [self.collectionView registerClass:[LRJDesFirCollectionViewCell class] forCellWithReuseIdentifier:@"LRJDesFirCollectionViewCell"];
    
#warning mark - 初始化
    self.dataArray = [NSMutableArray array];
    
    MBProgressHUD *hub = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [hub show:YES];
    hub.labelText = @"加载中";
    NSString *string = [NSString stringWithFormat:@"http://api.breadtrip.com/destination/index_places/%@",self.string];
    [AFNetworkTool getUrl:string body:nil result:HXJSON headerFile:nil success:^(id result) {
        
        NSMutableArray *array = result[@"data"];
        for (NSMutableDictionary *dic in array) {
            LRJDesFirDataModel *dataModel = [LRJDesFirDataModel modelWithDictionary:dic];
            [self.dataArray addObject:dataModel];
        }
        [self.collectionView reloadData];
        [hub hide:YES];
        
    } failure:^(NSError *error) {
        
        
    }];
    
    UIImage *leftImage = [UIImage imageNamed:@"btn_nav_back@2x.png"];
    leftImage = [leftImage imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftImage style:UIBarButtonItemStyleDone target:self action:@selector(leftButtonAction:)];
    
}

//返回
-(void)leftButtonAction:(UIBarButtonItem *)bar{
    
    [self.navigationController popToRootViewControllerAnimated:NO];
    
}


//多少个cell
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataArray.count;
}
//每行内容
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"LRJDesFirCollectionViewCell";
    LRJDesFirCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    LRJDesFirDataModel *dataModel = [self.dataArray objectAtIndex:indexPath.row];
    cell.dataModel = dataModel;
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    LRJDesSecCityViewController *desDetailVC = [[LRJDesSecCityViewController alloc]init];
    [self.navigationController pushViewController:desDetailVC animated:YES];
    LRJDesFirDataModel *dataModel = [self.dataArray objectAtIndex:indexPath.row] ;
    desDetailVC.type = dataModel.type;
    desDetailVC.id = dataModel.id;

}
    

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
